package com.easyedu.teacher.service;

import com.easyedu.core.entity.po.OpenCourse;
import com.easyedu.core.entity.po.StudentSelect;
import com.easyedu.core.entity.po.Teacher;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.teacher.dao.TeacherDao;
import com.easyedu.teacher.entity.dto.request.ReqCommitGrade;
import com.easyedu.teacher.entity.query.CommitGradeQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lmr
 **/
@Service
public class TeacherService {

    private final TeacherDao teacherDao;

    public TeacherService(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    public Teacher getInformation(Long teacherNumber) {
        Teacher information = teacherDao.getInformation(teacherNumber);
        if (information == null) {
            throw new EasyEduException(1999, "未查询到信息");
        }
        return information;
    }

    public List<OpenCourse> getTeachCourse(Long teacherId, int openYear, String openSemester) {
        return teacherDao.getTeachCourse(teacherId, openYear, openSemester);
    }

    @Transactional(rollbackFor = Exception.class)
    public int createGrade(List<ReqCommitGrade> reqCommitGrades) {
        int number = 0;
        for (ReqCommitGrade reqCommitGrade : reqCommitGrades) {
            CommitGradeQuery course = new CommitGradeQuery(reqCommitGrade.getSelectId(),
                    reqCommitGrade.getStudentNumber(),
                    reqCommitGrade.getStudentName(),
                    reqCommitGrade.getOpenCoursesId(),
                    reqCommitGrade.getCourseNumber(),
                    reqCommitGrade.getCourseName(),
                    reqCommitGrade.getUsualScore(),
                    reqCommitGrade.getExperimentScore(),
                    reqCommitGrade.getExamScore(),
                    reqCommitGrade.getTotalScore());
            Integer integer = teacherDao.updateGrade(course);
            if (integer != 1) {
                System.out.println(integer);
                throw new EasyEduException(1999, "提交成绩失败");
            }
            number++;
        }
        return number;
    }

    public List<StudentSelect> getCourseGrade(Long openCoursesId) {
        return teacherDao.getCourseGrade(openCoursesId);
    }
}
